import {userInfo, login} from '@/api/user';
// import loginAction from '@/utils/native/login'
import {getToken, removeToken, setToken} from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    userInfo: []

  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, userInfo) => {
      state.userInfo = userInfo
    }
  },

  actions: {
    // 登录
    Login({commit}, formData) {
      // const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login({
          orgId: formData.orgId,
          username: formData.username,
          password: formData.password
        }).then(response => {
          const data = response
          // const data = {
          //   'id': 22,
          //   'token': 'string',
          //   'detail': {
          //     'org_id': 0,
          //     'username': 'enixjin',
          //     'name': 'string',
          //     'phone': 'string',
          //     'gender': 0,
          //     'age': 0,
          //     'address': 'string',
          //     'id_no': 'string',
          //     'zip_code': 'string',
          //     'description': 'string',
          //     'role': 0,
          //     'birthday': '2020-05-16',
          //     'province': 'string',
          //     'city': 'string',
          //     'county': 'string',
          //     'telephone': 'string',
          //     'id': 22,
          //     'creator': 0,
          //     'updater': 0,
          //     'create_date': '2020-05-16',
          //     'update_date': '2020-05-16'
          //   }
          // }
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          localStorage.setItem('id', data.detail.id)
          // loginAction.login(data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({commit, state}) {
      return new Promise((resolve, reject) => {
        userInfo().then(response => {
          const data = response
          // if (data.permissions && data.permissions.length > 0) { // 验证返回的roles是否是一个非空数组
          // } else {
          //   reject('该用户还没有权限，请先联系管理员配置权限')
          //   // console.log('该用户还没有权限')
          // }
          commit('SET_ROLES', data.permissions)
          commit('SET_NAME', data.name)
          commit('SET_INFO', data)

          // commit('SET_AVATAR', data.avatar)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({commit, state}) {
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_INFO', [])

        removeToken()
        resolve()
      })
    },

    // 前端 登出
    FedLogOut({commit}) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
    // 动态修改权限
    ChangeRoles({commit}, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        userInfo(role).then(response => {
          const data = response
          commit('SET_ROLES', data.roles)

          // commit('SET_ROLES', data.permissions)

          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          resolve()
        })
      })
    },
    // 直接注入token

    InsertToken({commit}, token) {
      commit('SET_TOKEN', token)
      setToken(token)
    }

  }
}

export default user
